//使用会import很多模块，或者组件
import Vue from "vue";
//引入vue-router
import VueRouter from 'vue-router'
import routes from './routes'
import { isAuth } from '../utils/auth'

// 第三方库需要use一下才能用
Vue.use(VueRouter)

//定义routes路由的集合，数组类型
// const routes=[
//     //单个路由均为对象类型，path代表的是路径，component代表组件
//     {path:'/page1',component:page1},
//     {path:"/page2",component:page2}
// ]

let router = new VueRouter({
    mode:'history',
    // ES6简写，等于routes：routes
    routes
})


router.beforeEach((to,from,next)=>{
    let isLogined = isAuth()

    if(to.path === '/login'){
        if(isLogined){
            next('/')
        }else{
            next()
        }
    }else if(to.path === '/register'){
        if(isLogined){
            next('/')
        }else{
            next()
        }
    }else if(to.path === '/checknewPwd'){
        if(isLogined){
            next('/')
        }else{
            next()
        }
    }
    else{
        if(isLogined){
            next()
        }else{
            next('/login')
        }
    }
})

//抛出这个这个实例对象方便外部读取以及访问
export default router 






// https://www.cnblogs.com/dengyao-blogs/p/11562257.html